/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkrespnse;

import core.ParameterDecoder;
import utils.StringUtils;
import utils.Utils;
import utils.XMLParser;

/**
 *
 * @author Kvest
 */
public class SettingsXML extends Response  {
    private String settings;
    public SettingsData settingsData;
    
    public SettingsXML(byte[] data) {
        super(SETTINGSXML_RESPONSE);

        parse(data);
    }
    
    protected void parse(byte[] data)
    {
        int offset = 0;
        byte[] body;
        //��������  body
        body = ParameterDecoder.getTransferBody(data);
        
        //Пропускаем название пакета
        byte[] buffer4 = new byte[4];
        System.arraycopy(body, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        parse(body, offset);
    }
    
    public int parse(byte[] data, int offset)
    {
        byte[] buffer4 = new byte[4];  
        
        //xml
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int strSize = Utils.byteToInt(buffer4);
        byte[] buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        settings = StringUtils.bytesToStr(buffer);
      
        parseXML();
       
        return offset;
    }
    
    private void parseXML() {
        settingsData = new SettingsData();
        
        settingsData.T1 = XMLParser.getIntElement(settings, "FirstTimeSearch", settingsData.T1);
        settingsData.T2 = XMLParser.getIntElement(settings, "SecondTimeSearch", settingsData.T2);
        settingsData.T3 = XMLParser.getIntElement(settings, "ThirdTimeSearch", settingsData.T3);
        settingsData.maxSendCoordsTime = XMLParser.getIntElement(settings, "MaxSendCoordsTime", settingsData.maxSendCoordsTime);
        settingsData.javaVersion = XMLParser.getStringElement(settings, "JavaVersion", settingsData.javaVersion);
        settingsData.link = XMLParser.getStringElement(settings, "JavaNewVersionLink", settingsData.link);
        settingsData.maxOrdersCount = XMLParser.getIntElement(settings, "ConcurrentOrdersCount", settingsData.maxOrdersCount);
        settingsData.autoTariffClassUID = XMLParser.getIntElement(settings, "AutoTariffClassUID", settingsData.autoTariffClassUID);
        settingsData.checkNoSubOrderForClose = "true".equals(XMLParser.getStringElement(settings, "CheckNoSubOrderForClose", 
                                                                                        (settingsData.checkNoSubOrderForClose ? "true" : "false"))); 
    }
    
    public static class SettingsData {
        public int T1,T2,T3;
        public int maxSendCoordsTime;
        public String javaVersion;
        public String link;
        public int maxOrdersCount;
        public int autoTariffClassUID;
        public boolean checkNoSubOrderForClose;
        
        public SettingsData() {
            T1 = 60000;
            T2 = 60000;
            T3 = 60000;
            maxSendCoordsTime = 60000;
            javaVersion = null;
            link = null;
            maxOrdersCount = -1;
            autoTariffClassUID = 0;
            checkNoSubOrderForClose = true;
        }
    }
}
